package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.dao.MenuDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.GoodsTypeService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.util.StringUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {
    @Autowired
    GoodsTypeDao goodsTypeDao;
    @Autowired
    MenuDao menuDao;
    @Autowired
    LogService logService;
    @Autowired
    GoodsDao goodsDao;

    //商品管理中新增分类
    @Override
    public ServiceVO addGoodsType(String goodsTypeName, Integer pId) {
        //获取父类状态
        String parentState = goodsDao.getParentTypeState(pId);

        //添加分类
        goodsTypeDao.addGoodsType(goodsTypeName,pId);
        logService.save(new Log(Log.INSERT_ACTION,"添加商品分类:"+goodsTypeName));

        if (parentState != null && parentState.equals("0")){
            //更新父节点状态为1
            goodsTypeDao.updateGoodsTypeState(pId);
            logService.save(new Log(Log.UPDATE_ACTION,"修改商品分类状态:"+goodsTypeName));
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    //商品管理中删除分类
    @Override
    public ServiceVO deleteGoodsType(Integer goodsTypeId) {
        //获取父类状态
        Integer pId = goodsTypeDao.getGoodsTypeByTypeId(goodsTypeId);
//        String parentState = goodsDao.getParentTypeState(Integer.parseInt(pId));
        String parentState = goodsDao.getParentTypeState(pId);

        //删除分类
        goodsTypeDao.deleteGoodsType(goodsTypeId);
        logService.save(new Log(Log.DELETE_ACTION,"删除商品分类:"+goodsTypeId.toString()));

        if (parentState.equals("1")){
            //修改分类中父类的goods_type_state
            goodsTypeDao.updateGoodsTypeParentState(pId);
            logService.save(new Log(Log.UPDATE_ACTION,"修改商品分类状态:"+goodsTypeId.toString()));
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    //查询商品所有分类
    @Override
    public String getGoodsTree() {
        return this.getAllGoods(-1).toString();// 根节点默认从-1开始
    }

    /**
     * 递归查询当前角色下的所有菜单
     * @return
     */
    public JsonArray getAllGoods(Integer parentId){
        JsonArray array = this.getGoodsByParentId(parentId);

        for(int i = 0;i < array.size();i++){

            JsonObject obj = (JsonObject) array.get(i);

            if(obj.get("state").getAsString().equals("open")){//如果是叶子节点，不再递归

                continue;

            }else{//如果是根节点，继续递归查询

                obj.add("children", this.getAllGoods(obj.get("id").getAsInt()));

            }

        }

        return array;
    }

    /**
     * 根据父菜单ID获取菜单
     * @return
     */
    public JsonArray getGoodsByParentId(Integer parentId){

        JsonArray array = new JsonArray();

        List<GoodsType> goodsTypes = goodsTypeDao.getMenuByParentId(parentId);

        //遍历菜单
        for (GoodsType goodsType : goodsTypes) {

            JsonObject obj = new JsonObject();

            obj.addProperty("id", goodsType.getGoodsTypeId());// 菜单ID

            obj.addProperty("text", goodsType.getGoodsTypeName());// 菜单名称

            obj.addProperty("iconCls", "goods-type");//图标

            if(goodsType.getGoodsTypeState() == 0 ) {

                obj.addProperty("state", "open");// 叶子节点

            }else{

                obj.addProperty("state", "closed"); // 根节点

            }

            JsonObject attributes = new JsonObject(); //扩展属性

            attributes.addProperty("state", goodsType.getGoodsTypeState());

            obj.add("attributes", attributes);

            array.add(obj);

        }

        return array;
    }


}
